//
//  StatusViewModel.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/7.
//  Copyright © 2015年 002. All rights reserved.
//

#import "StatusViewModel.h"
#import "StatusCell.h"
#import "StatusRetweetedCell.h"

@implementation StatusViewModel

- (instancetype)initWithStatus:(Status *)status {
    
    if (self = [super init]) {
        self.status = status;
        
        NSArray<NSDictionary *> *urls = status.retweeted_status.pic_urls;
        if (!urls) {
            urls = status.pic_urls;
        }
        
        for (NSDictionary *dict in urls) {
            
            NSURL *url = [[NSURL alloc] initWithString:dict[@"thumbnail_pic"]];
            [self.thumbnailUrls addObject:url];
        }
//
//        if (self.status.pic_urls.count > 0) {
//            
//            for (NSDictionary *dict in self.status.pic_urls) {
//                
//                NSURL *url = [[NSURL alloc] initWithString:dict[@"thumbnail_pic"]];
//                [self.thumbnailUrls addObject:url];
//            }
//        }
    }
    
    return self;
}

+ (instancetype)initWithStatus:(Status *)status {
    return [[self alloc] initWithStatus:status];
}

// 针对 status 做的处理
- (void)setValue:(id)value forKey:(NSString *)key {
    
    if ([key isEqualToString:@"status"]) {
        
        NSDictionary *dict = value;
        if (dict) {
            self.status = [Status initWithDict:dict];
        }
        return;
    }
    
    [super setValue:value forKey:key];
}

// 用户头像地址
- (NSURL *)userProfileUrl {
    if (self.status.user.profile_image_url == nil) {
        return [[NSURL alloc] initWithString:@""];
    }
    return [[NSURL alloc] initWithString:self.status.user.profile_image_url];
}

// 用户默认头像
- (UIImage *)userDefaultImage {
    if (!_userDefaultImage) {
        _userDefaultImage = [UIImage imageNamed:@"avatar_default_big"];
    }
    return _userDefaultImage;
}

// 用户认证图像 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
- (UIImage *)userVipImage {
    switch (self.status.user.verified_type) {
        case 0:
            return [UIImage imageNamed:@"avatar_vip"];
            break;
        case 2:
        case 3:
        case 5:
            return [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 220:
            return [UIImage imageNamed:@"avatar_grassroot"];
            break;
        default:
           return nil;
    }
    return nil;
}

// 会员图标
- (UIImage *)userMemberImage {
    if (self.status.user.mbrank > 0 && self.status.user.mbrank < 7) {
        return [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%zd",self.status.user.mbrank]];
    }
    return nil;
}

- (NSMutableArray<NSURL *> *)thumbnailUrls {
    if (!_thumbnailUrls) {
        _thumbnailUrls = [[NSMutableArray<NSURL *> alloc] init];
    }
    return _thumbnailUrls;
}

/**
 *  模型调试信息
 *
 *  @return
 */
- (NSString *)description {
    return [NSString stringWithFormat:@"%@\n配图数组%@",self.status.description,self.thumbnailUrls];
}

// 缓存行高
- (CGFloat)rowHeight {
    
//    NSLog(@"计算缓存行高:%@",self.status.text);
    
    // 实例化 cell
    StatusRetweetedCell *cell = [[StatusRetweetedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StatusCellNormalId];
    
    // 返回行高
    return [cell rowHeight:self];
}

// 转发微博内容
- (NSString *)retweetedText {
    
    // 1. 判断是否是转发微博，如果不是直接返回 nil
    Status *s = self.status.retweeted_status;
    if (!s) {
        return nil;
    }
    // 2. s 就是转发微博  拼接转发内容
    return [NSString stringWithFormat:@"@%@:%@",self.status.user.screen_name,s.text];
}

// 微博id
- (NSString *)cellID {

    return self.status.retweeted_status != nil ? StatusRetweetedCellId : StatusCellNormalId;
}

@end
